package noyau;

import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ServantNotActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

import projetCORBA.Banque;
import projetCORBA.BanqueHelper;
import projetCORBA.Garant;
import projetCORBA.GarantHelper;
import projetCORBA.MauvaisMotDePasse;
import projetCORBA.Participant;
import projetCORBA.ParticipantHelper;
import projetCORBA.Stock;
import projetCORBA.StockHelper;
import projetCORBA.UtilisateurDejaExistant;

public class Client {
	public static void main (String[] args){
		org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
		org.omg.CORBA.Object objRef;
		
		Garant g = null;
		Banque b = null;
		Stock s = null;
		
		/* Instanciation participant */
		ParticipantImpl part = new ParticipantImpl("Tabou");
		
		try {
			objRef = orb.resolve_initial_references("NameService");
			
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
			
			g = GarantHelper.narrow(ncRef.resolve_str("GarantJBCM"));
			b = BanqueHelper.narrow(ncRef.resolve_str("BanqueJBCM"));
			s = StockHelper.narrow(ncRef.resolve_str("StockJBCM"));
	

			/* Enregistrement de l'utilisateur dans le service de nommage */
			POA rootpoa = POAHelper.narrow(orb
					.resolve_initial_references("RootPOA"));
			rootpoa.the_POAManager().activate();
			NameComponent path[];
			org.omg.CORBA.Object refs = rootpoa.servant_to_reference(part);
			Participant pref = ParticipantHelper.narrow(refs);
			path = ncRef.to_name(part.nom+"JBCM");
			ncRef.rebind(path, pref);
			/* Fin enregistrement */
			
		} catch (InvalidName e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (NotFound e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CannotProceed e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AdapterInactive e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServantNotActive e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WrongPolicy e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			part.motDePasse=g.creerCompte("Tabou");
			System.out.println("Mot de passe : "+part.motDePasse);
		} catch (UtilisateurDejaExistant e) {
			e.printStackTrace();
		}
		
		if (s.consulterStock(part.nom).length>0){
		System.out.println("Produits :"+s.consulterStock(part.nom)[0]);
		}
		try {
			System.out.println("Solde"+b.consulterSolde(part.nom,part.motDePasse));
		} catch (MauvaisMotDePasse e) {
			e.printStackTrace();
		}
	}
	
}
